package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.item.EvaluateItem;
import com.afkt.project.ui.adapter.EditsAdapter;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * detail: Adapter Item EditText 输入监听
 *
 * @author Ttt
 */
public class AdapterEditsActivity extends BaseActivity {

    private static final String TAG = "AdapterEditsActivity";

    // 适配器
    EditsAdapter editsAdapter;

    private ListContainer vidBvrList;

    private DirectionalLayout bar_layout;

    private DirectionalLayout bar;

    private Image adapter_return;

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_listContainer_edit;
    }

    @Override
    protected void initView() {

        //
        WindowManager.getInstance().getTopWindow().get().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);

        LogUtils.error(TAG, "initView");
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_edit_bar_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_edits_bar, null, false);
        adapter_return = (Image) bar.findComponentById(ResourceTable.Id_adapter_return);
        bar_layout.addComponent(bar);

        vidBvrList = (ListContainer) findComponentById(ResourceTable.Id_edit_bvr_list);

        //数据初始化
        List<EvaluateItem> lists = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            lists.add(new EvaluateItem());
        }
        // 默认清空第一条数据内容
        lists.get(0).evaluateContent = "";


        // 添加按钮
        Component view = QuickHelper.get(new BaseTextView(this))
                .setText("提交")
                .setBold()
                .setTextColor(ResourceUtils.getColorForInt(ResourceTable.Color_white))
                .setTextSize(40)
                .setPaddingLeft(30)
                .setPaddingRight(30)
                .setMarginTop(50)
                .setMarginLeft(50)
                .setOnClicks(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        StringBuilder builder = new StringBuilder();
                        for (EvaluateItem item : editsAdapter.getData()) {
                            builder.append("\nevaluateContent: ").append(item.evaluateContent);
                            builder.append("\nevaluateLevel: ").append(item.evaluateLevel);
                            builder.append("\n");
                        }
                        LogUtils.error(TAG, builder.toString());
                        ToastTintUtils.success("数据已打印, 请查看 Logcat");
                    }
                }).getView();
        bar.addComponent(view);
        ComponentContainer parent = vidBvrList;
        // 根布局处理
        ViewHelper.get().setPadding(parent, 0)
                .setBackgroundColor(parent, ResourceUtils.getColorForInt(ResourceTable.Color_color_33));


        // 初始化布局管理器、适配器
        editsAdapter = new EditsAdapter(lists, this);
        vidBvrList.setItemProvider(editsAdapter);

        LogUtils.error(TAG, "listview");
    }

    @Override
    protected void initListener() {
        adapter_return.setClickedListener(component -> onBackPressed());
    }

    @Override
    protected void onBackground() {
        //标题栏移除
        bar_layout.removeComponent(bar);
        super.onBackground();
    }
}
